package com.zsoa.common.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.Store;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.zsoa.common.base.EntityBase;


@Entity
@Table(name="app_user")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class User extends EntityBase {

	@ManyToOne(targetEntity=Department.class,cascade={CascadeType.REFRESH},fetch=FetchType.EAGER,optional=true)//多对一
	@JoinColumn(name="departmentIds",columnDefinition="bigint(20)",nullable=true)//外键名称
	private Department department;//用户所在的部门
	
	@Column(nullable=true,name="userName",length=50,columnDefinition="varchar(50)")
	@SearchableProperty(name="userName",index=Index.TOKENIZED,store=Store.YES,boost=1)//store是否存储; boost:优先级
	private String userName;//用户名
	
	@Column(nullable=true,name="passWord",length=50,columnDefinition="varchar(200)")
	@SearchableProperty(name="passWord",index=Index.NO,store=Store.YES,boost=1)//index:是否索引; store是否存储; boost:优先级
	private String passWord;//用户密码
	
	@Column(name="userClass",nullable=true,length=25,columnDefinition="varchar(25)")
	@SearchableProperty(name="userClass",index=Index.NO,store=Store.YES,boost=1)
	private String userClass;//用户分类
	
	@OneToOne(cascade={CascadeType.ALL},fetch=FetchType.EAGER,optional=true,targetEntity=UserInfo.class)//optional=true,optional声明关系是否是必须存在的,即是否允许其中一端为null
	@JoinColumn(name="userInfoId",columnDefinition="bigint(20)",unique=true,nullable=true)
	@SearchableComponent
	private UserInfo userInfo;//用户详细信息
	
	@ManyToMany(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER)
	@JoinTable(name="app_user_role",joinColumns={@JoinColumn(name="userId",referencedColumnName="id",columnDefinition="bigint(20)")},
			inverseJoinColumns={@JoinColumn(name="roleId",referencedColumnName="id",columnDefinition="bigint(20)")}
			)
	@OrderBy("createdDate desc, name asc")
	private Set<Role> roleSet = new HashSet<Role>();//用户拥有的角色
	
	
	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public String getUserClass() {
		return userClass;
	}

	public void setUserClass(String userClass) {
		this.userClass = userClass;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public Set<Role> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<Role> roleSet) {
		this.roleSet = roleSet;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
